import api from '../http/index.js'
import expireStorage from '@/util/storeUtil.js'


const token = {
	state: {
		accessToken: '',
		refreshToken: '',
	},

	mutations: {
		SET_ACCESS_TOKEN: (state, accessToken) => {
			state.accessToken = accessToken
			expireStorage.setCache('accessToken', accessToken, 12 * 3600)
		},
		SET_REFRESH_TOKEN: (state, refreshToken) => {
			state.refreshToken = refreshToken
			expireStorage.setCache('refreshToken', refreshToken, 7 * 24 * 3600)
		},
		REMOVE_ACCESS_TOKEN: (state) => {
			state.accessToken = ''
			expireStorage.removeCache('accessToken')
		},
		REMOVE_REFRESH_TOKEN: (state) => {
			state.refreshToken = ''
			expireStorage.removeCache('refreshToken')
		}
	},

	actions: {
		// 刷新token
		RefreshToken({commit,state}) {
			let headers = {
				"Content-Type": "application/json",
				"RefreshToken": expireStorage.getCache('refreshToken')
			}
			return api.refreshToken({header: headers})
		},


	}
}

export default token
